﻿using CodeCity.UserManagement.Service;
using CodeCity.UserManagement.Service.ApplicationModel;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CodeCity.UserManagement.Admin.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HumanController : ControllerBase
    {
        private readonly HumanService humanService;
        private readonly HouseService houseService;
        private readonly HosiptalService hosiptalService;

        public HumanController(HumanService humanService, HouseService houseService, HosiptalService hosiptalService)
        {
            this.humanService = humanService;
            this.houseService = houseService;
            this.hosiptalService = hosiptalService;
        }
        [HttpPost("register")]
        public async Task<IActionResult> RegisterAsync([FromBody] HumanRegisterModel model)
        {
            await humanService.RegisterAsync(model);
            return Created("", null);
        }
        [HttpPost("checkinWork")]
        public async Task<IActionResult> CheckinWorkAsync([FromBody] HumanWorkModel model)
        {
            await humanService.CheckinWorkAsync(model);
            return Ok();
        }
        [HttpPost("status")]
        public async Task<IActionResult> UpdateStatus([FromBody] UpdateStatusModel model)
        {
            await humanService.UpdateStatusAsync(model);
            return Ok();
        }

        [HttpPost("resignWork")]
        public async Task<IActionResult> ResignWorkAsync([FromBody] HumanWorkModel model)
        {
            await humanService.ResignWorkAsync(model);
            return Ok();
        }

        [HttpPost("chart")]
        public async Task<IActionResult> LoadChartAsync()
        {
            var humen = await humanService.LoadAllAsync();
            return Ok(humen.GroupBy(x => x.BirthDay.ToString("第 dd 天")).Select(x => new
            {
                x = x.Key,
                y = x.Count()
            }).ToList());
        }

        [HttpPost("list")]
        public async Task<List<HumanListItemModel>> GetHumenAsync()
        {
            var humen = await humanService.LoadAllAsync();
            return humen.Select(x =>
            {
                var human = new HumanListItemModel()
                {
                    FullName = x.FullName,
                    UnitId = x.UnitId,
                    Gender = x.Gender == Contract.Gender.Female ? "女" : "男"
                };
                human.HasWork = x.WorkingBuildingId > 0 ? "是" : "否";
                human.HasMarried = x.SpouseHumanId > 0 ? "是" : "否";
                human.HasHome = x.HomeBuildingId > 0 ? "是" : "否";
                if (x.IsBusy)
                {
                    switch (x.Status)
                    {
                        case Enums.HumanStatus.Building:
                            human.Status = "建造中";
                            break;
                        case Enums.HumanStatus.ForRest:
                            human.Status = "休息中";
                            break;
                        case Enums.HumanStatus.Gathering:
                            human.Status = "采集中";
                            break;
                        case Enums.HumanStatus.Treatment:
                            human.Status = "治疗中";
                            break;
                        case Enums.HumanStatus.Working:
                            human.Status = "工作中";
                            break;
                    }
                }
                else
                {
                    switch (x.Status)
                    {
                        case Enums.HumanStatus.Building:
                            human.Status = "去建造";
                            break;
                        case Enums.HumanStatus.ForRest:
                            human.Status = "去休息";
                            break;
                        case Enums.HumanStatus.Gathering:
                            human.Status = "去采集";
                            break;
                        case Enums.HumanStatus.Treatment:
                            human.Status = "去治疗";
                            break;
                        case Enums.HumanStatus.Working:
                            human.Status = "去工作";
                            break;
                    }
                }
                return human;
            }).ToList();
        }
    }
}
